<script lang="ts" setup>
import QrcodeVue from "qrcode.vue";

const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  link: {
    type: String,
    default: "",
  },
});

const emits = defineEmits(["update:show"]);

const clipboardSuccess = () => {
  ElMessage({
    showClose: true,
    message: "复制成功",
    type: "success",
  });
};

const copyItem = () => {
  let oInput = document.createElement("input");
  // 将想要复制的值
  oInput.value = props.link;
  // 页面底部追加输入框
  document.body.appendChild(oInput);
  // 选中输入框
  oInput.select();
  // 执行浏览器复制命令
  document.execCommand("Copy");
  // 复制后移除输入框
  ElMessage({
    showClose: true,
    message: "复制成功",
    type: "success",
  });
  oInput.remove();
};

const onClose = () => {
  emits("update:show", false);
};
</script>
<template>
  <Dialog :model-value="show" title="扫码预览" :width="500" @close="onClose">
    <div class="qrcode-container">
      <qrcode-vue :value="link" :size="200" level="H" />
      <div class="tips">
        <div>手机扫左边二维码预览</div>
        <div>
          你也可以打开
          <a :href="link" target="_blank">新窗口预览</a>
        </div>
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <!-- <el-button
          v-clipboard="`${link}`"
          v-clipboard:success="clipboardSuccess"
          >复制链接</el-button
        > -->
        <el-button @click="copyItem">复制链接</el-button>
        <el-button type="primary" @click="onClose">关闭</el-button>
      </span>
    </template>
  </Dialog>
</template>
<style lang="scss" scope>
.qrcode-container {
  display: flex;
  align-items: center;
  justify-content: center;

  .tips {
    color: #666666;
    margin-left: 20px;
    font-size: 14px;

    div {
      & + div {
        margin-top: 10px;
      }
    }

    a {
      color: var(--el-color-primary);
    }
  }
}
</style>
